package com.oracle.controller;

import com.oracle.utils.MD5Util;
import com.oracle.vo.MemberVo;
import com.oracle.vo.R;
import com.oracle.member.service.api.MemberServiceApi;
import com.oracle.pojo.dto.MemberDTO;
import com.oracle.vo.ResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired(required = false)
    private MemberServiceApi memberServiceApi;


    @RequestMapping("/mobile/getCode")
    public R mobileGetCode(String phone){
        ResultEntity resultEntity = this.memberServiceApi.createModileCode(phone);
        if (resultEntity.getCode().equals(ResultEntity.CODE_SUCCESS)){
            return new R(200,"ok");
        }
        return new R(201,resultEntity.getMsg());
    }

    @RequestMapping("/email/getCode")
    public R emailGetCode(String email){
        Map<String,Object> maps = new HashMap<String, Object>();
        ResultEntity resultEntity = this.memberServiceApi.createEmailCode(email);
        if (resultEntity.getCode().equals(ResultEntity.CODE_SUCCESS)){
            return new R(200,"ok");
        }
        return new R(201,resultEntity.getMsg());
    }

    @RequestMapping(value = "/email/reg",method = RequestMethod.POST)
    public R emailReg(@RequestBody MemberVo memberVo){
        //判断前端请求参数是否都有值
        if (memberVo.getUsername()==null || "".equals(memberVo.getUsername())){
            return new R(201,"用户名不能为空");
        }
        if (memberVo.getLoginname()==null || "".equals(memberVo.getLoginname())){
            return new R(201,"登录名不能为空");
        }
        if (memberVo.getPwd()==null || "".equals(memberVo.getPwd())){
            return new R(201,"密码不能为空");
        }
        if (memberVo.getEmail()==null || "".equals(memberVo.getEmail())){
            return new R(201,"邮箱地址不能为空");
        }
        //密码加密处理
        memberVo.setPwd(MD5Util.getMd5Str(memberVo.getPwd()));
        //把vo的内容传递给DTO数据传输层对象
        MemberDTO memberDTO = new MemberDTO();
        BeanUtils.copyProperties(memberVo,memberDTO);
        ResultEntity resultEntity = this.memberServiceApi.emailReg(memberDTO);
        if (resultEntity.getCode().equals(ResultEntity.CODE_SUCCESS)){
            return new R(200,"success");
        }
        return new R(resultEntity.getCode(),resultEntity.getMsg());
    }

    @RequestMapping(value = "/mobile/reg" , method = RequestMethod.POST)
    public R mobileReg(@RequestBody MemberVo memberVo){
        //判断前端请求参数是否都有值
        if (memberVo.getUsername()==null || "".equals(memberVo.getUsername())){
            return new R(201,"用户名不能为空");
        }
        if (memberVo.getLoginname()==null || "".equals(memberVo.getLoginname())){
            return new R(201,"登录名不能为空");
        }
        if (memberVo.getPwd()==null || "".equals(memberVo.getPwd())){
            return new R(201,"密码不能为空");
        }
        if (memberVo.getPhone()==null || "".equals(memberVo.getPhone())){
            return new R(201,"手机号不能为空");
        }
        //密码加密处理
        memberVo.setPwd(MD5Util.getMd5Str(memberVo.getPwd()));
        //把vo的内容传递给DTO数据传输层对象
        MemberDTO memberDTO = new MemberDTO();
        BeanUtils.copyProperties(memberVo,memberDTO);
        ResultEntity resultEntity = this.memberServiceApi.mobileReg(memberDTO);
        if (resultEntity.getCode().equals(ResultEntity.CODE_SUCCESS)){
            return new R(200,"success");
        }
        return new R(resultEntity.getCode(),resultEntity.getMsg());
    }


    @RequestMapping("/member/checkedLoginName")
    public R checkedLoginName(String loginname){
        ResultEntity resultEntity = this.memberServiceApi.checkedLoginName(loginname);
        if (resultEntity.getCode().equals(ResultEntity.CODE_SUCCESS)){
            return new R(200,"ok");
        }
        return new R(201,"error");
    }







}
